<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) 2015-2016 http://www.clcw.com.cn All rights reserved.    |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: Xmh <xiangminghu@clcw.com.cn>                                  |
// +------------------------------------------------------------------------+
//

namespace AuctionAppApi\Controller;

use Think\Controller;
use AuctionCore\System\AuctionException;
use AuctionCore\CarResource\CarReport;
use AuctionCore\Common\AntNest;
use AuctionCore\AuctionOrder\AuOrder;
use AuctionCore\Common\PhpRedis;
use AuctionCore\CarResource\Car;

/**
 * 检测报告 -- 分享版
 *
 * @author Xmh
 */
class ShareController extends BaseController {
    public $redis = null;
    public function __construct() {
        $this->h5_actions = array(
            'C_SHARE|A_REPAIR_SHARE'
        );
        parent::__construct();
        $this->redis = new PhpRedis();

    }

    /**
     * 车辆检测报告页  - H5
     * @param int $order_id
     */
    public function report(){
        $params = I('get.params');
        $arr = ['shorturl'=>$params];
        $keyName = getRedisKeyByCode('REDIS_KEY','100009',$arr);
        $parse_params = $this->redis->get($keyName);
        if(!$parse_params){
            AuctionException::make_throw('800050');
        }
        //起拍价
        $order = new AuOrder($parse_params);
        $order_detail = $order->detail();
        $car_id = $order_detail['car_id'];
        $this->report = new CarReport($car_id);
        //图片
        $car_core = new Car($car_id);
        $image = $car_core->get_pic_by_key($car_id, 'p_left_front45');
        $report = array();
        $report['car_pic'] = $image['pic_url'];
        $report['bid_start_price'] = format_money($order_detail['bid_start_price']).' 元';
        $report['header'] = $this->report->header();
        $report['header']['coupon'] = $order_detail['coupon'];
        $report['base_info'] = $this->report->lpcapp_base_info();
        $report['procedure'] = $this->report->procedure();
        $report['config'] = $this->report->configuration();
        $report['perform'] = $this->report->perform();
        $report['base_pics'] = $this->report->base_pics(true);
        foreach($report['base_pics'] as $k => $v){
            $report['base_pics'][$k]['pic_url'] = get_content_cdnurl().$v['pic_url'];
        }
        $report['damage_pics'] = $this->report->damage_pics();
        //外观与修复历史
        $report['appear_history'] = $this->report->appear_history();
        $report['frame_history'] = $this->report->frame_history();
        $report['appear11'] = $this->report->get_pos_class();
        $report['frame7'] = $this->report->get_pos_class('frame');
        //显性损伤
        $arr = $this->report->get_dm_point_class();
        $report['frame24'] = $arr['frame'];
        $report['outside41'] = $arr['outside'];
        $report['inside29'] = $arr['inside'];

        /**----------------可见损伤部位--start----------------------**/
        //可见损伤
        $report['stat']['visible'] = $this->report->get_dm_stat(); //可见损伤统计
        $report['stat']['degree'] = $this->report->get_dm_stat('dm_degree'); //损伤程度统计
        //

        //--------------------骨架可见损伤图片
        //轻微损伤图片(大图，轮播)
        $pics = $this->report->get_all_pics(true, 'frame');

        $report['frame_pics_big'] = json_decode($pics, true);
        foreach($report['frame_pics_big'] as $k =>$v){
            $report['frame_pics_big'][$k]['url']  =  get_content_cdnurl().$v['url'];
        }
        //轻微损伤图片(小图,轮播)
        $report['frame_pics_sma'] = array();
        foreach ($report['frame_pics_big'] as $k => $v) {
            $report['frame_pics_sma'][$k]['url'] = get_content_cdnurl().$v['url'] . '@200w_120h_4e';
        }
        //张数
        $report['frame_pics_count'] = count($report['frame_pics_big']);

        //--------------------外观可见损伤图片
        //轻微损伤图片(大图，轮播)
        $pics = $this->report->get_all_pics(true, 'outside');
        $report['outside_pics_big'] = json_decode($pics, true);
        foreach($report['outside_pics_big'] as $k =>$v){
            $report['outside_pics_big'][$k]['url']  =  get_content_cdnurl().$v['url'];
        }
        //轻微损伤图片(小图,轮播)
        $report['outside_pics_sma'] = array();
        foreach ($report['outside_pics_big'] as $k => $v) {
            $report['outside_pics_sma'][$k]['url'] = get_content_cdnurl().$v['url'] . '@200w_120h_4e';
        }
        //张数
        $report['outside_pics_count'] = count($report['outside_pics_big']);

        //--------------------内饰可见损伤图片
        //轻微损伤图片(大图，轮播)
        $pics = $this->report->get_all_pics(true, 'inside');
        $report['inside_pics_big'] = json_decode($pics, true);
        foreach($report['inside_pics_big'] as $k =>$v){
            $report['inside_pics_big'][$k]['url']  =  get_content_cdnurl().$v['url'];
        }
        //轻微损伤图片(小图,轮播)
        $report['inside_pics_sma'] = array();
        foreach ($report['inside_pics_big'] as $k => $v) {
            $report['inside_pics_sma'][$k]['url'] = get_content_cdnurl().$v['url'] . '@124w_74h_4e';
        }
        //张数
        $report['inside_pics_count'] = count($report['inside_pics_big']);

        /**----------------可见损伤部位--end----------------------**/


        /**----------------可见损伤部位(具体到部位)--start----------------------**/
        //骨架损伤部位列表
        $report['frame'] = $this->report->get_dm_type('3');
        $report['wg'] = $this->report->get_dm_type('2');
        $report['ns'] = $this->report->get_dm_type('1');
        if(!empty($report['frame'])){
            foreach($report['frame'] as $kf=>$vf) {
                $dm_pics = $this->report->get_dm_position_pics($vf['dm_id']);
                foreach($dm_pics['pictures'] as $k => $v){
                    $dm_pics['pictures'][$k]['img'] = get_content_cdnurl().$v['img'];
                }
                $report['frame'][$kf]['pictures'] = $dm_pics['pictures'];
            }
        }
        if(!empty($report['wg'])){
            foreach($report['wg'] as $kw=>$vw) {
                $dm_pics = $this->report->get_dm_position_pics($vw['dm_id']);
                foreach($dm_pics['pictures'] as $k => $v){
                    $dm_pics['pictures'][$k]['img'] = get_content_cdnurl().$v['img'];
                }
                $report['wg'][$kw]['pictures'] = $dm_pics['pictures'];
            }
        }
        if(!empty($report['ns'])){
            foreach($report['ns'] as $kn=>$vn) {
                $dm_pics = $this->report->get_dm_position_pics($vn['dm_id']);
                foreach($dm_pics['pictures'] as $k => $v){
                    $dm_pics['pictures'][$k]['img'] = get_content_cdnurl().$v['img'];
                }
                $report['ns'][$kn]['pictures'] = $dm_pics['pictures'];
            }
        }
        /**----------------可见损伤部位(具体到部位)--end----------------------**/


        /**----------------可见损伤程度--start----------------------**/

        //--------------------轻微损伤图片
        //报告页4张展示小图
        $a = $this->report->get_dm_pic('2');
        foreach ($a as $k => $v) {
            $v['url'] = get_content_cdnurl().$v['url'] . '@200w_150h_4e';
            $a[$k] = $v;
        }
        $report['light_pics'] = $a;
        unset($k);
        unset($v);

        //轻微损伤图片(大图，轮播)
        $pics = $this->report->get_all_pics(true, 'light');
        $report['light_pics_big'] = json_decode($pics, true);
        foreach($report['light_pics_big'] as $k =>$v){
            $report['light_pics_big'][$k]['url']  =  get_content_cdnurl().$v['url'];
        }
        //轻微损伤图片(小图,轮播)
        $report['light_pics_sma'] = array();
        foreach ($report['light_pics_big'] as $k => $v) {
            $report['light_pics_sma'][$k]['cus_pos_name'] = $v['cus_pos_name']?$v['cus_pos_name']:C('VISIBLE_DAMAGE_POSITION.'.$v['position']);
            $report['light_pics_sma'][$k]['describe'] = $v['describe'];
            $report['light_pics_sma'][$k]['url'] = $v['url'] . '@200w_150h_4e';
        }

        //张数
        $report['light_pics_count'] = count($report['light_pics_big']);

        //---------------------一般损伤图片
        //报告页4张展示小图
        $b = $this->report->get_dm_pic('3');
        foreach ($b as $k => $v) {
            $v['url'] = get_content_cdnurl().$v['url'] ;
            $b[$k] = $v;
        }
        $report['normal_pics'] = $b;
        unset($k);
        unset($v);

        //一般损伤图片(大图，轮播)
        $pics = $this->report->get_all_pics(true, 'normal');
        $report['normal_pics_big'] = json_decode($pics, true);
        foreach($report['normal_pics_big'] as $k =>$v){
            $report['normal_pics_big'][$k]['url']  =  get_content_cdnurl().$v['url'];
        }
        //一般损伤图片(小图,轮播)
        $report['normal_pics_sma'] = array();
        foreach ($report['normal_pics_big'] as $k => $v) {
            $report['normal_pics_sma'][$k]['cus_pos_name'] = $v['cus_pos_name']?$v['cus_pos_name']:C('VISIBLE_DAMAGE_POSITION.'.$v['position']);
            $report['normal_pics_sma'][$k]['describe'] = $v['describe'];
            $report['normal_pics_sma'][$k]['url'] = $v['url']. '@200w_150h_4e';
        }
        //张数
        $report['normal_pics_count'] = count($report['normal_pics_big']);

        //----------------------重要损伤图片
        //报告页4张展示小图
        $c = $this->report->get_dm_pic('4');
        foreach ($c as $k => $v) {
            $v['url'] = get_content_cdnurl().$v['url'] . '@200w_120h_4e';
            $c[$k] = $v;
        }
        $report['important_pics'] = $c;
        unset($k);
        unset($v);

        //重要损伤图片(大图，轮播)
        $pics = $this->report->get_all_pics(true, 'important');
        $report['important_pics_big'] = json_decode($pics, true);
        foreach($report['important_pics_big'] as $k =>$v){
            $report['important_pics_big'][$k]['url']  =  get_content_cdnurl().$v['url'];
        }
        //重要损伤图片(小图,轮播)
        $report['important_pics_sma'] = array();
        foreach ($report['important_pics_big'] as $k => $v) {
            $report['important_pics_sma'][$k]['cus_pos_name'] = $v['cus_pos_name']?$v['cus_pos_name']:C('VISIBLE_DAMAGE_POSITION.'.$v['position']);
            $report['important_pics_sma'][$k]['describe'] = $v['describe'];
            $report['important_pics_sma'][$k]['url'] = $v['url'] . '@200w_150h_4e';
        }
        //张数
        $report['important_pics_count'] = count($report['important_pics_big']);
        //print_r($report);die;
        /**----------------可见损伤程度--end----------------------**/
        $this->assign('report',$report);
        $this->display();
    }

    /**
     * 车辆检测报告页  - Share Url
     * @param int $car_id
     */
    public function report_share(){
        $params = I('post.order_id/d',0);
        if(empty($params)){
            AuctionException::make_throw('800050');
        }
        $short_param = short_url($params);
        $is_short_param = $this->redis->get($short_param);
        if(!$is_short_param){
            $this->redis->set($short_param,$params);
        }
        $order = new AuOrder($params);
        $order_detail = $order->detail();
        $car_id = $order_detail['car_id'];
        $this->report = new CarReport($car_id);
        //图片
        $car_core = new Car($car_id);
        $image = $car_core->get_pic_by_key($car_id, 'p_left_front45');
        $car_header = $this->report->header();
        $share_url =  "http://www.lpaiche.com/lpcapp" . U("Share/report", ['params'=>$short_param]);
        $share = array();
        $share['title'] = $car_header['model_show'];
        $share['url'] = $share_url;
        $share['desc'] = $car_header['composite_state_desc'].$car_header['acc_level_desc'];
        $share['image'] = $image['pic_url'];
        $this->api_200($share);
    }

    //维保记录分享页接口
    /**
     * @param $rid
     */
    public function share_repair(){
        $rid = I('post.rid');
        $short_param = short_url($rid);
        if(empty($rid)){
            AuctionException::make_throw('8000002');
        }
        $arr = ['shorturl'=>$short_param];
        $keyName = getRedisKeyByCode('REDIS_KEY','100009',$arr);
        $is_short_param = $this->redis->get($keyName);
        if(!$is_short_param){
            $this->redis->set($keyName,$rid);
        }
        $ant = new AntNest();
        $option = array(
            'reportId' => $rid
        );
        $data = $ant->getRepairDetail($option);
        /*$vin = $data['introduction']['vin'];
        $arr = $repair->get_cars($vin);
        $cm = new \AuctionCore\CarResource\CarModel();
        $brand = $cm->get_model_by_id($arr['brand_id'], 'brand');*/
        $brand = $data['introduction']['manufacturer'].' '.$data['introduction']['model'].' '.$data['introduction']['transmissiontype'].' '.$data['introduction']['displacement']; //获取车辆的名称
        $share = array();
        $share_url =  "http://cdn.lpaiche.com/lpcapp" . U("Share/repair_share", ['rid'=>$short_param]);
        $share['title'] = $brand.'|维修保养记录查询|来拍车';
        $share['desc'] = "【来拍车】 水淹、调表、事故、火烧鉴别。安心无忧";
        $share['image'] = "http://appapi.lpaiche.com/images/clcw.png";
        $share['url'] = $share_url;
        $this->api_200($share);
    }

    //维保分享维修保养记录 h5 页面
    public function repair_share(){
        $rid = trim(I('get.rid'));
        $arr = ['shorturl'=>$rid];
        $keyName = getRedisKeyByCode('REDIS_KEY','100009',$arr);
        $parse_params = $this->redis->get($keyName);
        if(!$parse_params){
            AuctionException::make_throw('800050');
        }
        $ant = new AntNest();
        $option = array(
            'reportId' => $parse_params
        );
        $data = $ant->getRepairDetail($option);
        $data['introduction']['manufacturer_1'] = $data['introduction']['manufacturer'];
        $data['introduction']['manufacturer'] = $data['introduction']['manufacturer'].' '.$data['introduction']['model'].' '.$data['introduction']['transmissiontype'].' '.$data['introduction']['displacement'];
        $record = $data['normalrepairrecords'];
        $introduction = $data['introduction'];
        $introduction['vin'] = substr($introduction['vin'],0,-4)."****";
        $this->assign('record',$record);
        $this->assign('introduction',$introduction);
        $this->display();
    }

}